#include<Windows.h>
#include<process.h>
#include <iostream>
using namespace std;

HANDLE hEvent;

DWORD WINAPI Thread1(LPVOID param){
	char ch = (char)param;
	WaitForSingleObject(hEvent, INFINITE);
	
	for(int i = 0; i < 10; i++){
		cout << ch;
		Sleep(500);
	}
	SetEvent(hEvent);	//event is free
	return 0;
}

void main(){

	hEvent = CreateEvent(NULL, 
						FALSE,/*TRUE - manualreset; FALSE - autoreset*/
						TRUE,/*FALSE - not free*/
						"MyEvent"/*name*/
						);

	ResetEvent(hEvent);	//event is not free
	cout << "Thread creation!!!!!!!!!!!\n";


	HANDLE hThread1 = CreateThread(NULL, 0, Thread1, (LPVOID)'A', NULL, NULL);
	HANDLE hThread2 = CreateThread(NULL, 0, Thread1, (LPVOID)'B', NULL, NULL);

	Sleep(3000);
	cout << "\n=============== SetEvent =================\n";
	
	cout << "Main is waiting......\n";
	SetEvent(hEvent);
	

	
	

	WaitForSingleObject(hThread2, INFINITE);


}